import {loginByUsername,getUserInfo,signOut,refreshToken} from '@/api/user';
import {getCookies,setCookies,removeCookies} from "@/utils/cookies";

export default {
    namespaced:true,
    state:{
        uid:'',
        name:'',
        email:'',
        avatar:'',
        status:'',
        cookies:getCookies(),
        token_createAt:'',
        access_token:'',
        refresh_token:'',
        expires_in:''
    },
    mutations:{
        SET_UID(state,uid){
            state.uid=uid;
        },
        SET_NAME(state,name){
            state.name=name;
        },
        SET_EMAIL(state,email){
            state.email=email;
        },
        SET_AVATAR(state,avatar){
            state.avatar=avatar;
        },
        SET_STATUS(state,status){
            state.status=status;
        },
        SET_TOKEN_CREATE_AT(state,time){
            state.token_createAt=time;
        },
        SET_ACCESS_TOKEN(state,token){
            state.access_token=token;
        },
        SET_REFRESH_TOKEN(state,token){
            state.refresh_token=token;
        },
        SET_EXPIRES_IN(state,expires_in){
            state.expires_in=expires_in;
        },
        CLEAR_STATE(state){
            state.uid='';
            state.name='';
            state.email='';
            state.avatar='';
            state.status='';
            state.cookies='';
            state.token_createAt='';
            state.access_token='';
            state.refresh_token='';
            state.expires_in='';
        }
    },
    getters:{
        is_expires(state){
            return Date.now()-state.token_createAt>1000*state.expires_in;
        }
    },
    actions:{
        loginByUsername({commit},{name,password}){

            return new Promise((resolve,reject)=>{

                loginByUsername({name,password}).then(res=>{
                    console.log('loginByUsername data:',res.data);

                    if(0!=res.data.code)return reject(res.data.msg);

                    var data=res.data.data;
                    commit('SET_UID',data.uid);
                    commit('SET_NAME',data.name);

                    commit('SET_TOKEN_CREATE_AT',Date.now());
                    commit('SET_ACCESS_TOKEN',data.access_token);
                    commit('SET_REFRESH_TOKEN',data.refresh_token);
                    commit('SET_EXPIRES_IN',data.expires_in);
                    setCookies(data.access_token);

                    resolve();
                }).catch(err=>{
                    reject(err);
                });

            });
        },
        getUserInfo({commit}){
            return new Promise((resolve,reject)=>{
                getUserInfo().then(res=>{
                    console.log('getUserInfo res:',res);
                    commit('SET_NAME',res.data.data.name);
                    commit('SET_EMAIL',res.data.data.email);
                    commit('SET_AVATAR',res.data.data.avatar);
                    commit('SET_STATUS',res.data.data.status);
                    resolve();
                }).catch(err=>{
                    console.error('getUserInfo err:',err);

                    reject(err);
                });
            });
        },
        FedSignOut({commit}){
            return new Promise(resolve=>{
                commit('CLEAR_STATE');
                removeCookies();
                resolve();
            });
        },
        signOut({commit}){
            console.log('SignOut');
            return new Promise((resolve,reject)=>{
                signOut().then(()=>{
                    commit('CLEAR_STATE');
                    removeCookies();
                    resolve();
                }).catch(err=>{
                    reject(err);
                });
            });
        },
        refreshToken({commit,state}){
            return new Promise((resolve,reject)=>{
                console.log('refreshToken');
                refreshToken(state.refresh_token).then(res=>{
                   commit('SET_TOKEN_CREATE_AT',Date.now());
                   commit('SET_ACCESS_TOKEN',res.data.data.access_token);
                   commit('SET_REFRESH_TOKEN',res.data.data.refresh_token);
                   commit('SET_EXPIRES_IN',res.data.data.expires_in);
                }).catch(err=>{
                    reject(err);
                });
            });
        }
    }
}